package com.ll.wechat.base.activity;

import com.ll.wechat.R;
import com.ll.wechat.base.utils.ActivityManager;
import com.ll.wechat.base.utils.dao.IBaseActivity;
import com.ll.wechat.base.utils.dao.IBaseActivityManager;
import com.ll.wechat.base.utils.thread.ThreadPoolUtil;
import com.ll.wechat.base.utils.widget.MyToast;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

/**
 * Activity的封装基类
 * @ClassName: BaseActivity 
 * @author yeliangliang
 * @date 2015-7-22 下午1:43:39
 */
/**
 * BaseActivity要做的一些事情： 1、将所有的Activity统一的管理起来。 2、将创建、结束、跳转的方法进行封装，统一管理。
 * 3、写一些共用的代码，避免重复劳动。
 */
public abstract class BaseActivity extends FragmentActivity implements IBaseActivity,
		IBaseActivityManager {
	
	private long exitTime = 0;
	private long lastTime = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 创建了新的Activity，将其存入集合
		if (!ActivityManager.list.contains(this)) {
			ActivityManager.list.add(this);
		}
		initView();
		initData();
	}

	@Override
	public void finishActivity(Activity activity) {
		ActivityManager.list.remove(activity);
		activity.finish();
	}

	@Override
	public void skipActivity(Activity oldActivity, Class<?> newActivity) {
		showActivity(oldActivity, newActivity);
		ActivityManager.list.remove(oldActivity);
		oldActivity.finish();
	}

	@Override
	public void showActivity(Activity oldActivity, Class<?> newActivity) {
		Intent intent = new Intent(oldActivity, newActivity);
		startActivity(intent);
	}
	@Override
	public void showActivity(Activity oldActivity, Class<?> newActivity, Bundle bundle) {
		Intent intent = new Intent(oldActivity, newActivity);
		intent.putExtras(bundle);
		startActivity(intent);
	}
	public void exitApp(Context oldActivity) {
		exitTime = System.currentTimeMillis()-lastTime;
		if (exitTime <= 2000) {
			//释放所有Activity
			for (Activity a :ActivityManager.list) {
				a.finish();
			}
			//关闭所有正在执行的线程
			ThreadPoolUtil.closeAllThreadPool();
			System.exit(0);
		}else {
			lastTime = System.currentTimeMillis();
			MyToast.showToast(oldActivity,R.string.exitApp);
		}
	}

}
